#include "crate.h"
#include "../framework.h"

/* Crate Views */
/*******************************************************************************/

Vertexf CrateViewA::vertices[] = {	Vertexf(-0.5f,-0.5f),Vertexf(0.5f,-0.5f),
									Vertexf(0.5f,0.5f),Vertexf(-0.5f,0.5f),
									Vertexf(-0.4f,-0.4f),Vertexf(0.4f,-0.4f),
									Vertexf(0.4f,0.4f),Vertexf(-0.4f,0.4f)};


Vertexf CrateViewB::vertices[] = {	Vertexf(-0.5f,-0.5f),Vertexf(0.5f,-0.5f),
									Vertexf(0.5f,0.5f),Vertexf(-0.5f,0.5f),
									Vertexf(-0.5f,-0.43f),Vertexf(0.5f,-0.42f),
									Vertexf(-0.5f,-0.30f),Vertexf(0.5f,-0.32f),
									Vertexf(-0.5f,-0.25f),Vertexf(0.5f,-0.25f),
									Vertexf(-0.5f,-0.12f),Vertexf(0.5f,-0.10f),
									Vertexf(-0.5f,-0.05f),Vertexf(0.5f,-0.05f),
									Vertexf(-0.5f,0.09f),Vertexf(0.5f,0.07f),
									Vertexf(-0.5f,0.20f),Vertexf(0.5f,0.14f),
									Vertexf(-0.5f,0.31f),Vertexf(0.5f,0.25f),
									Vertexf(-0.5f,0.45f),Vertexf(0.5f,0.46f)};

Vertexf CrateViewC::vertices[] = {	Vertexf(-0.5f,0.0f),Vertexf(-0.25f,-0.5f),
									Vertexf(0.25f,-0.5f),Vertexf(0.5f,0.0f),
									Vertexf(0.25f,0.5f),Vertexf(-0.25f,0.5f)};

void CrateViewA::draw(){
	Framework& fr = Framework::instance();

	Colorf oldColor = fr.pipeline.getFillColor();
	fr.pipeline.setFillColor(Colorf(0.31f,0.25f,0.15f));
	fr.pipeline.draw(Pipeline::POLYGON,CrateViewA::vertices,4);
	fr.pipeline.setFillColor(Colorf(0.76f,0.61f,0.4f));
	fr.pipeline.draw(Pipeline::POLYGON,CrateViewA::vertices+4,4);
	fr.pipeline.setFillColor(oldColor);
}

void CrateViewB::draw(){
	Framework& fr = Framework::instance();

	Colorf oldColorf = fr.pipeline.getFillColor();
	Colorf oldColor = fr.pipeline.getLineColor();

	fr.pipeline.setFillColor(Colorf(0.31f,0.25f,0.15f));
	fr.pipeline.draw(Pipeline::POLYGON,CrateViewB::vertices,4);

	fr.pipeline.pushMatrix();
		fr.pipeline.setFillColor(Colorf(0.0f,0.0f,0.0f));
		fr.pipeline.escale(0.9f,0.9f);
		fr.pipeline.draw(Pipeline::POLYGON,CrateViewB::vertices,4);
	fr.pipeline.popMatrix();

	fr.pipeline.setLineColor(Colorf(0.76f,0.61f,0.4f));
	fr.pipeline.draw(Pipeline::LINES,CrateViewB::vertices+4,18);
	fr.pipeline.setLineColor(oldColor);
	fr.pipeline.setFillColor(oldColorf);
}

void CrateViewC::draw(){
	Framework& fr = Framework::instance();

	Colorf oldColor = fr.pipeline.getFillColor();

	fr.pipeline.setFillColor(Colorf(0.4f,0.4f,0.4f));
	fr.pipeline.draw(Pipeline::POLYGON,CrateViewC::vertices,6);
	fr.pipeline.pushMatrix();
		fr.pipeline.setFillColor(Colorf(0.61f,0.62f,0.61f));
		fr.pipeline.escale(0.8f,0.8f);
		fr.pipeline.draw(Pipeline::POLYGON,CrateViewC::vertices,6);
	fr.pipeline.popMatrix();

	fr.pipeline.setFillColor(oldColor);
}

/*******************************************************************************/

Crate::Crate(Rectangle rec,CrateView& view)
	: 	SceneObject(rec),
		Grabable(Rectangle(rec.x1,rec.x2,rec.y1-10.0f,rec.y1)),
		Solid(rec),view(view){
	this->objVertices.vertices = NULL;
	this->objVertices.nVertices = 0;
	this->grabbed=false;
}

Crate::~Crate(){
}

void Crate::draw(){
	Framework& fr = Framework::instance();
	fr.pipeline.pushMatrix();
		fr.pipeline.translate(this->dimensions.centerX(),this->dimensions.centerY());
		fr.pipeline.escale(this->dimensions.width(),this->dimensions.height());

		view.draw();
	fr.pipeline.popMatrix();
}

void Crate::timeStep(){
	if(!grabbed){
		this->dimensions.y1 = this->getSize().y1;
		this->dimensions.y2 = this->getSize().y2;
		this->setGrabZone(Rectangle(this->dimensions.x1,this->dimensions.x2,this->dimensions.y1-10,this->dimensions.y1));
	}
}

void Crate::move(float dx,float dy){
	SceneObject::move(dx,dy);
	this->grabZone.translate(dx,dy);
	this->getSize().translate(dx,dy);
}
